package com.woniu.supermarket.web;

import com.github.pagehelper.PageInfo;
import com.woniu.supermarket.entity.Employee;
import com.woniu.supermarket.entity.LoginLog;
import com.woniu.supermarket.result.ResultResponse;
import com.woniu.supermarket.service.IEmployeeService;
import com.woniu.supermarket.service.ILoginLogService;
import com.woniu.supermarket.util.CodeCreateUtil;
import com.woniu.supermarket.util.IpUtil;
import com.woniu.supermarket.util.JwtUtil;
import com.woniu.supermarket.vo.EmpVo;
import com.woniu.supermarket.vo.EmployeeVo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;

@RestController
@RequestMapping("employee")
@Api(value = "用户登录")
public class EmpController {
    @Autowired
    IEmployeeService employeeService;

    @Autowired
    ILoginLogService iLoginLogService;

    @PostMapping("/login")
    public ResultResponse<Employee> login(@ApiParam(name="empVo",value="接收用户名和密码")
                                         @RequestBody EmpVo empVo, HttpServletRequest request){
        ResultResponse<Employee> result = null;

        Employee employee = employeeService.login(empVo.getUsername());
        if (employee == null){
            result = new ResultResponse<>(201,"账户不存在");
        }else if (employee.getPassword().equals(empVo.getPassword())){
            //登录日志
            String code = employee.getEmployeeNumber();
            String ip = IpUtil.getIpAddr(request);
            String cityInfo = null;

            LoginLog loginLog = new LoginLog();
            loginLog.setIp(ip);
            loginLog.setAddress(cityInfo);
            loginLog.setName(empVo.getUsername());
            loginLog.setEmpCode(code);
            iLoginLogService.addLog(loginLog);

            EmpVo emp = new EmpVo();
            empVo.setUsername(empVo.getUsername());
            String token = JwtUtil.createToken(emp);
            result = new ResultResponse<>(200,"登录成功!",employee,token);
        }else {
            result = new ResultResponse<>(202,"密码错误");
        }

        return result;
    }
    //分页查询
    @GetMapping("/elist")
    @ApiOperation(value = "分页查询员工信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前页", dataType = "int", paramType = "query", name = "now"),
            @ApiImplicitParam(value = "分页单位", dataType = "int", paramType = "query", name = "size"),
            @ApiImplicitParam(value = "按照状态查询条件", dataType = "Integer", paramType = "query", name = "status"),
            @ApiImplicitParam(value = "按照编码查询条件", dataType = "String", paramType = "query", name = "employeeNumber"),
    })
    public ResultResponse<PageInfo<EmployeeVo>> elist(@RequestParam(name = "now",defaultValue = "1") Integer now,
                                                      @RequestParam(name = "size",defaultValue = "3") Integer size,
                                                      Integer status,
                                                      String employeeNumber){

        ResultResponse<PageInfo<EmployeeVo>> result = null;
        try {
            PageInfo<EmployeeVo> pageInfo = employeeService.findEmployeeByCons(now, size, employeeNumber,status);
            result = new ResultResponse<>(200,"查询员工信息成功",pageInfo);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询员工信息失败");
        }
        return result;

    }


    @PostMapping("/add")
    @ApiOperation(value = "添加员工操作")
    public ResultResponse<EmployeeVo> addEmployee(@ApiParam(value = "接收员工对象")@RequestBody EmployeeVo employeeVo){
        ResultResponse<EmployeeVo> result = null;

        try {
            int i  = employeeService.addEmployee(employeeVo);
            if(i>0){
                result = new ResultResponse<>(200,"添加员工成功",employeeVo);
            }else{
                result = new ResultResponse<>(201,"添加员工失败");
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(555,"系统错误");
        }
        return result;
    }


    @PutMapping("/modify")
    @ApiOperation(value = "修改员工操作")
    public ResultResponse<Void> modifyWarehousing(@RequestBody EmployeeVo employeeVo) {
        ResultResponse<Void> result = null;
        try {
            int i = employeeService.modifyEmployee(employeeVo);
            result = new ResultResponse<>(200, "修改员工成功");
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "修改员工失败");
        }
        return result;
    }


    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "删除员工操作")
    public ResultResponse<Void> delEmployee(@PathVariable("id")Integer id) {
        ResultResponse<Void> result = null;
        try {
            employeeService.removeEmployee(id);
            result = new ResultResponse<>(200, "删除员工成功");
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "删除员工失败");
        }
        return result;
    }

    @DeleteMapping("/delBatch")
    @ApiOperation(value = "批量删除员工操作")
    public ResultResponse<Void> delBatch(@RequestParam("ids")int[] ids) {
        ResultResponse<Void> result = null;
        try {
            employeeService.removeBatchIds(ids);
            result = new ResultResponse<>(200, "批量删除员工成功");
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "批量删除员工失败");
        }
        return result;
    }
    @GetMapping("/addcode")
    public ResultResponse<String> addcode() {
        ResultResponse<String> result = null;
        try {
            String code = CodeCreateUtil.createEmployeeCode();
            result = new ResultResponse<>(200, "生成编码成功！",code);
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "生成编码失败！");
        }
        return result;
    }
}
